package com.wei.czz.framework.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.wei.czz.common.enums.CommonEnum;
import com.wei.czz.common.po.UserPo;
import com.wei.czz.common.utils.SecurityUtils;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.wei.czz.framework.admin.dao.UserTokenDao;
import com.wei.czz.framework.admin.entity.UserTokenEntity;
import com.wei.czz.framework.admin.service.UserTokenService;

/**
 * Created by IntelliJ IDEA.
 *
 * @author wei
 * date: 2024-06-10 11:07:32
 * className: UserTokenServiceImpl
 * version: 1.0
 * description:
 */
@Service("userTokenService")
@AllArgsConstructor
public class UserTokenServiceImpl extends ServiceImpl<UserTokenDao, UserTokenEntity> implements UserTokenService {

    private static final Logger log = LoggerFactory.getLogger(UserTokenServiceImpl.class);

    @Override
    public void saveOrEdit(UserTokenEntity userToken) {
        Long id = userToken.getId();
        if (Objects.isNull(id)) {

            int count = baseMapper.insert(userToken);
            log.info("插入用户令牌完成。count={}", count);
        } else {

            int count = baseMapper.updateById(userToken);
            log.info("修改用户令牌完成。count={}", count);
        }
    }

    @Override
    public List<UserTokenEntity> findList(Long userId, Integer channel, Long timing) {
        LambdaQueryWrapper<UserTokenEntity> userTokenLambdaQueryWrapper = new LambdaQueryWrapper<>();
        userTokenLambdaQueryWrapper.eq(UserTokenEntity::getUserId, userId)
                .eq(UserTokenEntity::getChannel, channel)
                .gt(UserTokenEntity::getExpireTime, timing)
                .eq(UserTokenEntity::getDeleteStatus, CommonEnum.ZERO.getValue());
        // 查询
        List<UserTokenEntity> userTokenList = baseMapper.selectList(userTokenLambdaQueryWrapper);
        log.info("查询用户令牌完成，size={}", userTokenList.size());
        return userTokenList;
    }

    @Override
    public void clearOther(Long userId, Integer channel, Date timing) {
        Long optUserId = CommonEnum.ZERO.getLongValue();
        String optUserName = StringUtils.EMPTY;
        Date date = new Date();
        try {
            UserPo userPo = SecurityUtils.getUser();
            optUserId = userPo.getUserId();
            optUserName = userPo.getUsername();
        } catch (Exception e) {
            log.info("没有登录用户。message={}", e.getMessage());
        }

        LambdaUpdateWrapper<UserTokenEntity> userTokenLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        userTokenLambdaUpdateWrapper.set(UserTokenEntity::getExpireTime, date.getTime())
                .set(UserTokenEntity::getUpdateTime, date)
                .set(UserTokenEntity::getUpdateUser, optUserName)
                .set(UserTokenEntity::getUpdateUserId, optUserId);
        userTokenLambdaUpdateWrapper.eq(UserTokenEntity::getUserId, userId)
                .eq(UserTokenEntity::getChannel, channel)
                .gt(UserTokenEntity::getExpireTime, timing.getTime())
                .lt(UserTokenEntity::getCreateTime, timing)
                .eq(UserTokenEntity::getDeleteStatus, CommonEnum.ZERO.getValue());
        // 删除
        int count = baseMapper.update(userTokenLambdaUpdateWrapper);
        log.info("删除用户令牌完成。count={}", count);
    }
}